#!/usr/bin/env bash
##############################################################
# File Name: centos7_install_hadoop.sh
# Version: V1.0
# Author: Chinge_Yang
# Blog: http://ygqygq2.blog.51cto.com
# Created Time : 2018-11-21 15:47:41
# Description:
# 1. 提前手动设置好hostname
# 2. 在server0，即第一台执行此脚本
##############################################################

##############################################################
# 是否安装集群，false为添加节点，true为安装集群
INSTALL_CLUSTER="true"
# 安装目录
INSTALL_BASE_DIR="/opt"
HADOOP_BASE_DIR=$INSTALL_BASE_DIR/hadoop
# 定义版本信息
JAVA_VERSION="1.8.0"
HADOOP_VERSION="2.8.5"
FLINK_VERSION="1.6.2"
# 主机名:IP，需要执行脚本前设置hostname
server0="hdp-master1:192.168.105.72"
server1="hdp-slave2:192.168.105.73"
server2="hdp-slave3:192.168.105.74"
##############################################################
NAMES=("${server0%:*}" "${server1%:*}" "${server2%:*}")
HOSTS=("${server0#*:}" "${server1#*:}" "${server2#*:}")
##############################################################

PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
sourcepackage_dir=/data/packages
makework_dir=/data/packages/src
mkdir -p ${sourcepackage_dir}
mkdir -p ${makework_dir}

# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please use root to run me."
exit 1
fi
##############################################################

#获取脚本所存放目录
cd `dirname $0`
SH_DIR=`pwd`
ME=$0
PARAMETERS=$*
# 定义ssh参数
ssh_port="22"
ssh_parameters="-o StrictHostKeyChecking=no -o ConnectTimeout=60"
ssh_command="ssh ${ssh_parameters} -p ${ssh_port}"
scp_command="scp ${ssh_parameters} -P ${ssh_port}"

# 定义日志
install_log=/root/install_log.txt

#定义输出颜色函数
function red_echo () {
#用法:  red_echo "内容"
    local what="$*"
    echo -e "$(date +%F-%T) \e[1;31m ${what} \e[0m"
}

function green_echo () {
#用法:  green_echo "内容"
    local what="$*"
    echo -e "$(date +%F-%T) \e[1;32m ${what} \e[0m"
}

function yellow_echo () {
#用法:  yellow_echo "内容"
    local what="$*"
    echo -e "$(date +%F-%T) \e[1;33m ${what} \e[0m"
}

function blue_echo () {
#用法:  blue_echo "内容"
    local what="$*"
    echo -e "$(date +%F-%T) \e[1;34m ${what} \e[0m"
}

function twinkle_echo () {
#用法:  twinkle_echo $(red_echo "内容")  ,此处例子为红色闪烁输出
local twinkle='\e[05m'
local what="${twinkle} $*"
echo -e "$(date +%F-%T) ${what}"
}

function return_echo () {
if [ $? -eq 0 ]; then
    echo -n "$* " && green_echo "成功"
    return 0
else
    echo -n "$* " && red_echo "失败"
    return 1
fi
}

function return_error_exit () {
[ $? -eq 0 ] && local REVAL="0"
local what=$*
if [ "$REVAL" = "0" ];then
    [ ! -z "$what" ] && { echo -n "$* " && green_echo "成功" ; }
else
    red_echo "$* 失败，脚本退出"
    exit 1
fi
}

# 定义确认函数
function user_verify_function () {
while true;do
    echo ""
    read -p "是否确认?[Y/N]:" Y
    case $Y in
        [yY]|[yY][eE][sS])
            echo -e "answer:  \\033[20G [ \e[1;32m是\e[0m ] \033[0m"
            break
        ;;
        [nN]|[nN][oO])
            echo -e "answer:  \\033[20G [ \e[1;32m否\e[0m ] \033[0m"
            exit 1
        ;;
        *)
            continue
    esac
done
}

# 定义跳过函数
function user_pass_function () {
while true;do
    echo ""
    read -p "是否确认?[Y/N]:" Y
    case $Y in
        [yY]|[yY][eE][sS])
            echo -e "answer:  \\033[20G [ \e[1;32m是\e[0m ] \033[0m"
            break
            ;;
        [nN]|[nN][oO])
            echo -e "answer:  \\033[20G [ \e[1;32m否\e[0m ] \033[0m"
            return 1
            ;;
        *)
            continue
    esac
done
}


function print_sys_info() {
    cat /etc/issue
    cat /etc/*-release
    uname -a
    MemTotal=`free -m | grep Mem | awk '{print  $2}'`
    echo "Memory is: ${MemTotal} MB "
    df -h
}

function set_timezone() {
    blue_echo "Setting timezone..."
    rm -rf /etc/localtime
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
}

function disable_selinux() {
    if [ -s /etc/selinux/config ]; then
        setenforce 0
        sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
    fi
}

function check_hosts() {
    if grep -Eqi '^127.0.0.1[[:space:]]*localhost' /etc/hosts; then
        echo "Hosts: ok."
    else
        echo "127.0.0.1 localhost.localdomain localhost" >> /etc/hosts
    fi
    ping -c1 www.baidu.com
    if [ $? -eq 0 ] ; then
        echo "DNS...ok"
    else
        echo "DNS...fail, add dns server to /etc/resolv.conf"
        cat > /etc/resolv.conf <<EOF
nameserver 114.114.114.114
nameserver 8.8.8.8
EOF
        echo '添加DNS done!'>>${install_log}
    fi
}

function ready_yum() {
    # 添加yum源
    [ ! -d /etc/yum.repos.d/bak/ ] && { mkdir /etc/yum.repos.d/bak/ ;mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/ ; }    
    cat > /etc/yum.repos.d/CentOS-Base.repo <<EOF
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-\$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
    http://mirrors.aliyuncs.com/centos/\$releasever/os/\$basearch/
    http://mirrors.cloud.aliyuncs.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates 
[updates]
name=CentOS-\$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/
    http://mirrors.aliyuncs.com/centos/\$releasever/updates/\$basearch/
    http://mirrors.cloud.aliyuncs.com/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-\$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/
    http://mirrors.aliyuncs.com/centos/\$releasever/extras/\$basearch/
    http://mirrors.cloud.aliyuncs.com/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-\$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/
    http://mirrors.aliyuncs.com/centos/\$releasever/centosplus/\$basearch/
    http://mirrors.cloud.aliyuncs.com/centos/\$releasever/centosplus/\$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-\$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/contrib/\$basearch/
    http://mirrors.aliyuncs.com/centos/\$releasever/contrib/\$basearch/
    http://mirrors.cloud.aliyuncs.com/centos/\$releasever/contrib/\$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7    
EOF

    cat > /etc/yum.repos.d/epel-7.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 7 - \$basearch
baseurl=http://mirrors.aliyun.com/epel/7/\$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - \$basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/\$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0

[epel-source]
name=Extra Packages for Enterprise Linux 7 - \$basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
EOF

}

function init_install() {
    print_sys_info
    set_timezone
    disable_selinux
    check_hosts
    ready_yum
}

function check_system () {
clear
printf "Checking system config now......\n"

SUCCESS="\e[1;32m检测正常\e[0m"
FAILURE="\e[1;31m检测异常\e[0m"
UNKNOWN="\e[1;31m未检测到\e[0m"
UPGRADE="\e[1;31m装服升级\e[0m"

#检查CPU型号
CPUNAME=$(awk -F ': ' '/model name/ {print $NF}' /proc/cpuinfo|uniq|sed 's/[ ]\{3\}//g')
[[ -n "$CPUNAME" ]] && CPUNAMEACK="$SUCCESS" || CPUNAMEACK="$UNKNOWN"

#检查物理CPU个数
CPUNUMBER=$(grep 'physical id' /proc/cpuinfo | sort -u | wc -l)
[[ "$CPUNUMBER" -ge "1" ]] && CPUNUMBERACK="$SUCCESS" || CPUNUMBERACK="$FAILURE"

#检查CPU核心数
CPUCORE=$(grep 'core id' /proc/cpuinfo | sort -u | wc -l)
[[ "$CPUCORE" -ge "1" ]] && CPUCOREACK="$SUCCESS" || CPUCOREACK="$FAILURE"

#检查线程数
CPUPROCESSOR=$(grep 'processor' /proc/cpuinfo | sort -u | wc -l)
[[ "$CPUPROCESSOR" -ge "1" ]] && CPUPROCESSORACK="$SUCCESS" || CPUPROCESSORACK="$FAILURE"

#检查内存大小
MEMSIZE=$(awk '/MemTotal/{print ($2/1024/1024)"GB"}' /proc/meminfo)
[[ $(echo ${MEMSIZE%G*}|awk '{if($0>=4)print $0}') ]] && MEMSIZEACK="$SUCCESS" || MEMSIZEACK="$FAILURE"


function CHECK_DISK_SIZE () {
    #检查硬盘大小
    DSKSIZE=($(parted -l 2>/dev/null|grep Disk|grep '/dev/'|grep -v mapper|awk '{print $2 $3}'))
    for DS in ${DSKSIZE[@]}; do
        [[ $(echo ${DSKSIZE%G*}|awk -F':' '{if($2>=50)print $2}') ]] && DSKSIZEACK="$SUCCESS" || DSKSIZEACK="$FAILURE"
        printf "$DSKSIZEACK 硬盘大小:           $DS\n"
    done
}

#检查根分区可用大小
DSKFREE=$(df -h / |awk 'END{print $(NF-2)}')
[[ $(echo ${DSKFREE%G*}|awk '{if($0>=50)print $0}') ]] && DSKFREEACK="$SUCCESS" || DSKFREEACK="$FAILURE"

function CHECK_NETWORK_CARD () {
#获取网卡名
cd /etc/sysconfig/network-scripts/
IFCFGS=($(ls ifcfg-*|awk -F'-' '{print $2}'|egrep -v "lo|.old"|awk -F':' '{print $1}'))

for IFCFG in ${IFCFGS[@]} ; do
    #检查网卡类型,暂时不检测
    ETHTYPE=$(ethtool -i $IFCFG|awk '/driver:/{print $NF}')
    [[ "$ETHTYPE" = "XXXX" ]] && ETHTYPEACK="$SUCCESS" || ETHTYPEACK="$FAILURE"
    ETHTYPEACK="$SUCCESS"

    #检查网卡驱动版本,暂时不检测
    DRIVERS=$(ethtool -i $IFCFG|awk '{if($1=="version:") print $NF}')
    [[ "$DRIVERS" = "XXXX" ]] && DRIVERSACK="$SUCCESS" || DRIVERSACK="$UPGRADE"
    DRIVERSACK="$SUCCESS"

    #检查网卡速率
    ETHRATE=$(ethtool $IFCFG|awk '/Speed:/{print $NF}')
    [[ "${ETHRATE/"Mb/s"/}" -ge "1000" ]] && ETHRATEACK="$SUCCESS" || ETHRATEACK="$FAILURE"

    printf "$ETHTYPEACK ${IFCFG}网卡类型:           $ETHTYPE\n"
    printf "$DRIVERSACK ${IFCFG}网卡驱动版本:               $DRIVERS\n"
    printf "$ETHRATEACK ${IFCFG}网卡速率:           $ETHRATE\n"
done
}

#检查服务器生产厂家
SEROEMS=$(dmidecode |grep -A4 "System Information"|awk -F': ' '/Manufacturer/{print $NF}')
[[ -n "$SEROEMS" ]] && SEROEMSACK="$SUCCESS" || SEROEMSACK="$UNKNOWN"

#检查服务器型号
SERTYPE=$(dmidecode |grep -A4 "System Information"|awk -F': ' '/Product/{print $NF}')
[[ -n "$SERTYPE" ]] && SERTYPEACK="$SUCCESS" || SERTYPEACK="$UNKNOWN"

#检查服务器序列号
SERSNUM=$(dmidecode |grep -A4 "System Information"|awk -F': ' '/Serial Number/{print $NF}')
[[ -n "$SERSNUM" ]] && SERSNUMACK="$SUCCESS" || SERSNUMACK="$UNKNOWN"

#检查IP个数
IPADDRN=$(ip a|grep -v "inet6"|awk '/inet/{print $2}'|awk '{print $1}'|\
egrep -v '^127\.'|awk -F/ '{print $1}' |wc -l)
[[ $IPADDRN -ge 1 ]] && IPADDRS=($(ip a|grep -v "inet6"|\
    awk '/inet/{print $2}'|awk '{print $1}'|egrep -v '^127\.'|awk -F/ '{print $1}'))
[[ $IPADDRN -ge 1 ]] && IPADDRP=$(echo ${IPADDRS[*]}|sed 's/[ ]/,/g')
[[ $IPADDRN -ge 1 ]] && IPADDRNACK="$SUCCESS" || IPADDRNACK="$FAILURE"

#检查操作系统版本
OSVERSI=$(cat /etc/redhat-release)
[[ $(echo $OSVERSI | grep 'CentOS Linux') ]] && OSVERSIACK="$SUCCESS" || OSVERSIACK="$FAILURE"

#检查操作系统类型
OSTYPES=$(uname -i)
[[ $OSTYPES = "x86_64" ]] && OSTYPESACK="$SUCCESS" || OSTYPESACK="$FAILURE"

#检查系统运行等级
OSLEVEL=$(runlevel)
[[ "$OSLEVEL" =~ "3" ]] && OSLEVELACK="$SUCCESS" || OSLEVELACK="$FAILURE"

function CHECK_DISK_SPEED () {
    twinkle_echo $(yellow_echo "Will check disk speed ......")
    user_pass_function
    [ $? -eq 1 ] && return 1
    yum -y install hdparm  # 先安装测试工具
    #检查硬盘读写速率
    DISKHW=($(hdparm -Tt $(fdisk -l|grep -i -A1 device|awk 'END{print $1}')|awk '{if(NR==3||NR==4)print $(NF-1),$NF}'))
    #Timing cached reads
    CACHEHW=$(echo ${DISKHW[*]}|awk '{print $1,$2}')
    [[ $(echo $CACHEHW|awk '{if($1>3000)print $0}') ]] && CACHEHWACK="$SUCCESS" || CACHEHWACK="$FAILURE"
    #Timing buffered disk reads
    BUFFRHW=$(echo ${DISKHW[*]}|awk '{print $3,$4}')
    [[ $(echo $BUFFRHW|awk '{if($1>100)print $0}') ]] && BUFFRHWACK="$SUCCESS" || BUFFRHWACK="$FAILURE"

    printf "$CACHEHWACK 硬盘cache读写速率:          $CACHEHW\n"
    printf "$BUFFRHWACK 硬盘buffer读写速率:         $BUFFRHW\n"
}

#检查时区
OSZONES=$(date +%Z)
[[ "$OSZONES" = "CST" ]] && OSZONESACK="$SUCCESS" || OSZONESACK="$FAILURE"

#检查DNS配置
yum -y install bind-utils
DNS=($(awk '{if($1=="nameserver") print $2}' /etc/resolv.conf))
DNSCONF=$(echo ${DNS[*]}|sed 's/[ ]/,/g')
[[ $(grep "\<nameserver\>" /etc/resolv.conf) ]] && DNSCONFACK="$SUCCESS" || DNSCONFACK="$FAILURE"
if [[ $(nslookup www.baidu.com|grep -A5 answer|awk '{if($1=="Address:") print $2}') ]];then
    DNSRESO=($(nslookup www.baidu.com|grep -A5 answer|awk '{if($1=="Address:") print $2}'))
    DNSRESU=$(echo ${DNSRESO[*]}|sed 's/[ ]/,/g')
    DNSRESOACK="$SUCCESS"
else
    DNSRESU="未知"
    DNSRESOACK="$FAILURE"
fi

#检查SElinux状态
SELINUX=$(sestatus |awk -F':' '{if($1=="SELinux status") print $2}'|xargs echo)
if [[ $SELINUX = disabled ]];then
    SELINUXACK="$SUCCESS"
else
    SELINUXACK="$FAILURE"
    sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
fi
HOSTNAME=$(hostname)
if [[ $HOSTNAME != "localhost.localdomain" ]];then
        HostNameCK="$SUCCESS"
else
        HostNameCK="$FAILURE"
fi
#打印结果
printf "\n"
printf "检测结果如下：\n"
printf "===========================================================================\n"
printf "$CPUNAMEACK CPU型号:            $CPUNAME\n"
printf "$CPUNUMBERACK   CPU个数:            $CPUNUMBER\n"
printf "$CPUCOREACK CPU核心数:          $CPUCORE\n"
printf "$CPUPROCESSORACK    CPU进程数:          $CPUPROCESSOR\n"
printf "$MEMSIZEACK 内存大小:           $MEMSIZE\n"
CHECK_DISK_SIZE
printf "$DSKFREEACK 根分区可用大小:         $DSKFREE\n"
printf "$SEROEMSACK 服务器生产厂家:         $SEROEMS\n"
printf "$SERTYPEACK 服务器型号:         $SERTYPE\n"
printf "$SERSNUMACK 服务器序列号:           $SERSNUM\n"
CHECK_NETWORK_CARD
printf "$IPADDRNACK 配置网卡IP数:           $IPADDRN个 $IPADDRP \n"
printf "$OSVERSIACK 操作系统版本:           $OSVERSI\n"
printf "$OSTYPESACK 操作系统类型:           $OSTYPES\n"
printf "$OSLEVELACK 系统运行等级:           $OSLEVEL\n"
printf "$OSZONESACK 系统时区:           $OSZONES\n"
printf "$DNSCONFACK DNS配置:            $DNSCONF\n"
printf "$DNSRESOACK DNS解析结果:            $DNSRESU\n"
printf "$SELINUXACK SElinux状态:            $SELINUX\n"
printf "$HostNameCK 主机名检测:         $HOSTNAME\n"
# CHECK_DISK_SPEED
printf "\n"
[[ $SELINUX = disabled ]] || printf "%30s\e[1;32mSElinux状态已修改,请重启系统使其生效.\e[0m\n"
printf "===========================================================================\n"
printf "系统分区情况如下:\n\n"
df -hPT -xtmpfs
printf "\n"
[[ $(df -hPT -xtmpfs|grep -A1 Filesystem|awk 'END{print $1}'|wc -L) -gt 9 ]] && printf "%30s\033[1;32m提示:存在LVM分区\033[0m\n"
printf "===========================================================================\n"
sleep 15
}

function system_opt () {
    yellow_echo "进行系统优化："
    # user_pass_function
    # [ $? -eq 1 ] && return 1

    init_install

    # 安装基本工具
    yum -y install openssh-clients wget rsync
    #修改SSH为允许用key登录
    mkdir -p /root/.ssh/
    chmod -R 700 /root/.ssh/
    echo '创建ssh登录所用的目录---done!'>>${install_log}
    
    # sed -i "s#PasswordAuthentication yes#PasswordAuthentication no#g"  /etc/ssh/sshd_config
    sed -i "s@#UseDNS yes@UseDNS no@" /etc/ssh/sshd_config
    sed -i 's/.*LogLevel.*/LogLevel DEBUG/g' /etc/ssh/sshd_config
    sed -i 's@#MaxStartups 10@MaxStartups 50@g' /etc/ssh/sshd_config
    # sed -i 's@#PermitRootLogin yes@PermitRootLogin no@g' /etc/ssh/sshd_config
    service sshd reload
    echo '设置ssh免密登录 done!'>>${install_log}
    
    # 关闭防火墙
    systemctl disable firewalld
    systemctl stop firewalld
    echo '关闭防火墙 done!' >>${install_log}

    #关闭，开启一些服务
    systemctl enable crond
    systemctl start crond

    # 设置.bashrc
    cat > /root/.bashrc <<EOF
# .bashrc

# User specific aliases and functions

alias rm='rm --preserve-root -i'
alias cp='cp -i'
alias mv='mv -i'
alias rz='rz -b'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
export LANG=en_US.UTF-8
export PS1="[\u@\h \W]\\\\$ "

ulimit -c unlimited
ulimit -n 40960
EOF
    echo '设置.bashrc done!'>>${install_log}

    #设置.bash_profile
    if ! grep 'df' /root/.bash_profile > /dev/null; then
    cat >> /root/.bash_profile <<EOF
echo '=========================================================='
cat /etc/redhat-release
echo '=========================================================='
df -lh
EOF
    fi
    echo '设置.bash_profile done!'>>${install_log}

    # 修改系统语言
    echo 'LANG="en_US.UTF-8"' > /etc/locale.conf
    echo '设置系统语言 done!'>>${install_log}

    # 更新bash，修复漏洞
    yum -y update bash

    #设置chrony服务
    yum -y install ntpdate
    ntpdate ntp1.aliyun.com
    cat > /etc/chrony.conf <<EOF    
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony
EOF
    systemctl start chronyd 
    systemctl enable chronyd
    echo '设置时区，同步时间 done! '>>${install_log}

    #修改最大的连接数为40960，重启之后就自动生效。
    ! grep "*                soft   nofile          40960" /etc/security/limits.conf > /dev/null \
    && echo '*                soft   nofile          40960'>>/etc/security/limits.conf

    ! grep "*                hard   nofile          40960" /etc/security/limits.conf > /dev/null \
    && echo '*                hard   nofile          40960'>>/etc/security/limits.conf
    ########################################
    ! grep 'HISTFILESIZE=2000' /etc/bashrc > /dev/null && echo 'HISTFILESIZE=2000'>>/etc/bashrc
    ! grep 'HISTSIZE=2000' /etc/bashrc > /dev/null && echo 'HISTSIZE=2000'>>/etc/bashrc
    ! grep 'HISTTIMEFORMAT="%Y%m%d-%H:%M:%S: "' /etc/bashrc > /dev/null && echo 'HISTTIMEFORMAT="%Y%m%d-%H:%M:%S: "'>>/etc/bashrc
    ! grep 'export HISTTIMEFORMAT' /etc/bashrc > /dev/null && echo 'export HISTTIMEFORMAT'>>/etc/bashrc
    ########################################
}

function install_java () {
#################################################################################################
    java -version && { yellow_echo "Already have a java" ; return 0 ; }
    cd ${makework_dir}
    tar -zxvf ${sourcepackage_dir}/jdk-8*-linux-x64.tar.gz
    mv jdk1.8.0_* /usr/local/
    ln -sf /usr/local/jdk1.8.0_* /usr/local/java
    ln -sf /usr/local/jdk1.8.0_* /usr/local/jdk
    ln -sf /usr/local/java/bin/java /usr/local/sbin/

    if ! grep "JAVA_HOME" /etc/profile > /dev/null; then
    cat >> /etc/profile <<EOF
JAVA_HOME=/usr/local/java
JAVA_BIN=\$JAVA_HOME/bin
JRE_HOME=\$JAVA_HOME/jre
JRE_BIN=\$JRE_HOME/bin
PATH=\$PATH:\$JAVA_BIN
CLASSPATH=\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
EOF
fi

    source /etc/profile
    echo 'install java done! '>>${install_log}
#################################################################################################    
}

function install_hadoop() {
#################################################################################################    
    # 添加hosts
    echo $server0|awk -F ':' '{print $2" "$1}' >> /etc/hosts
    echo $server1|awk -F ':' '{print $2" "$1}' >> /etc/hosts
    echo $server2|awk -F ':' '{print $2" "$1}' >> /etc/hosts

    ! grep HADOOP_CONF_DIR /etc/profile > /dev/null && cat >> /etc/profile << EOF
HADOOP_SSH_OPTS="$ssh_parameters"
HADOOP_CONF_DIR=$HADOOP_BASE_DIR/etc/hadoop
export HADOOP_SSH_OPTS HADOOP_CONF_DIR
EOF
    source /etc/profile

    if [ "$HOSTNAME" = "${NAMES[0]}" ]; then
        # 免交互生成ssh key
        [ ! -f ~/.ssh/id_rsa ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
        for h in ${HOSTS[@]}; do
            ssh-copy-id ${ssh_parameters} -p ${ssh_port} -i ~/.ssh/id_rsa root@${h}
        done
    fi    
     
    cd $INSTALL_BASE_DIR
    [ ! -d $HADOOP_BASE_DIR ] && \
        tar -zxvf $sourcepackage_dir/hadoop-${HADOOP_VERSION}.tar.gz \
        || return_echo "Already install a hadoop"
    mv hadoop-${HADOOP_VERSION} hadoop
    mkdir -p $HADOOP_BASE_DIR/tmp
    mkdir -p $HADOOP_BASE_DIR/hdfs/name
    mkdir -p $HADOOP_BASE_DIR/hdfs/data

    echo "export JAVA_HOME=${JAVA_HOME}" >> $HADOOP_CONF_DIR/hadoop-env.sh

    cat > $HADOOP_CONF_DIR/core-site.xml <<EOF
<configuration>
<!--配置hdfs文件系统的命名空间-->
  <property>
  <name>fs.defaultFS</name>
 <value>hdfs://${NAMES[0]}:9000</value>
  </property>
<!-- 配置操作hdfs的存冲大小 -->
  <property>
  <name>io.file.buffer.size</name>
 <value>4096</value>
  </property>
<!-- 配置临时数据存储目录 -->
  <property>
  <name>hadoop.tmp.dir</name>
  <value>$HADOOP_BASE_DIR/tmp</value>
  </property>
</configuration>
EOF

    cat > $HADOOP_CONF_DIR/hdfs-site.xml <<EOF
<configuration>
<!--配置副本数-->
  <property>
  <name>dfs.replication</name>
  <value>3</value>
  </property>
<!--hdfs的元数据存储位置-->
  <property>
  <name>dfs.namenode.name.dir</name>
  <value>$HADOOP_BASE_DIR/hdfs/name</value>
 </property>
<!--hdfs的数据存储位置-->
 <property>
  <name>dfs.datanode.data.dir</name>
  <value>$HADOOP_BASE_DIR/hdfs/data</value>
 </property>
<!--hdfs的namenode的web ui 地址-->
 <property>
  <name>dfs.http.address</name>
 <value>${NAMES[0]}:50070</value>
  </property>
<!--hdfs的snn的web ui 地址-->
  <property>
 <name>dfs.secondary.http.address</name>
  <value>${NAMES[0]}:50090</value>
  </property>
<!--是否开启web操作hdfs-->
 <property>
  <name>dfs.webhdfs.enabled</name>
 <value>true</value>
  </property>
<!--是否启用hdfs权限（acl）-->
  <property>
  <name>dfs.permissions</name>
  <value>false</value>
  </property>
</configuration>
EOF

    cat > $HADOOP_CONF_DIR/mapred-site.xml <<EOF
<configuration>
<!--指定maoreduce运行框架-->
    <property>
        <name>mapreduce.framework.name</name>
        <value>yarn</value>
    </property>
<!--历史服务的通信地址-->
    <property>
        <name>mapreduce.jobhistory.address</name>
        <value>${NAMES[0]}:10020</value>
    </property>
<!--历史服务的web ui地址-->
    <property>
        <name>mapreduce.jobhistory.webapp.address</name>
        <value>${NAMES[0]}:19888</value>
    </property>
</configuration>
EOF

    cat > $HADOOP_CONF_DIR/slaves <<EOF
$(echo ${NAMES[@]}|sed 's/ /\n/g')    
EOF
#################################################################################################
}

function install_flink() {
    return 0
    # cd $INSTALL_BASE_DIR
    # tar -zxvf /data/packages/flink-1.6.2-*
    # mv flink-1.6.2-* flink
}

function do_all() {
    # 第一台master节点
    if [[ "$INSTALL_CLUSTER" != "false" && "$HOSTNAME" = "${NAMES[0]}" ]]; then
        check_running=$(ps aux|grep "/bin/bash /tmp/$(basename $ME)"|grep -v grep)
        if [ -z "$check_running" ]; then  # 为空表示非远程执行脚本
            for ((i=$((${#HOSTS[@]}-1)); i>=0; i--)); do
                $ssh_command root@${HOSTS[$i]} "yum -y install rsync"
                # 将脚本分发至master节点
                rsync -avz -e "${ssh_command}" $ME  root@${HOSTS[$i]}:/tmp/
                $ssh_command root@${HOSTS[$i]} "/bin/bash /tmp/$(basename $ME)"
            done
        else
            # 系统检查
            check_system
            # 系统优化
            system_opt
            # 安装
            install_java
            install_hadoop

            # 启动
            $HADOOP_BASE_DIR/bin/hdfs namenode -format
            $HADOOP_BASE_DIR/sbin/start-dfs.sh
            $HADOOP_BASE_DIR/sbin/start-yarn.sh
        fi
    else
        # 系统检查
        check_system
        # 系统优化
        system_opt
        # 安装
        install_java
        install_hadoop
    fi
}


do_all
# 执行完毕的时间
green_echo "本次安装花时:$SECONDS 秒"
echo '完成安装 '>>${install_log}
